package com.younion.dao;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Maps;
import com.younion.common.base.BaseDAO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.*;

 
public class MemberCardDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(MemberCardDAO.class);

	 
	public Collection selectAll() throws BaseException {
		return queryForList("t_Member_Card.selectAll", null);
	}

	 
	public Collection selectByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return null;
		try {
			return queryForList("t_Member_Card.selectByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_Member_Card.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_Member_Card.selectAll";
			} else {
				sqlStr = "t_Member_Card.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public MemberCardVO insertByVo(MemberCardVO originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			return (MemberCardVO) this.insert("t_Member_Card.insertByVo",
					originalVo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public MemberCardVO[] insertVos(MemberCardVO[] originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			for (int i = 0; i < originalVo.length; i++)
				originalVo[i] = insertByVo(originalVo[i]);
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public Collection insertByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				MemberCardVO data = (MemberCardVO) it.next();
				data = insertByVo(data);
			}
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return false;
		try {
			this.update("t_Member_Card.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateVos(Map[] originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			for (int i = 0; i < originalVo.length; i++) {
				if (!updateByMap(originalVo[i]))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				Map data = (Map) it.next();
				if (!updateByMap(data))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteAll() throws BaseException {
		try {
			return this.getSqlMapClientTemplate().delete(
					"t_Member_Card.deleteAll", null) > 0 ? true : false;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public MemberCardVO selectById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return null;
		try {
			return (MemberCardVO) this.getSqlMapClientTemplate()
					.queryForObject("t_Member_Card.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Member_Card.deleteById",
					paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByIds(List list) throws BaseException {
		if (CollectionUtils.isEmpty(list))
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Member_Card.deleteByIds",
					list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Member_Card.deleteByMap",
					map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public boolean recharge(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate()
					.update("t_Member_Card.recharge", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateCouponAmounts(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().update(
					"t_Member_Card.updateCouponAmounts", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateCouponAmount(Map map) throws BaseException {
		if (map == null || map.isEmpty())
			return false;
		try {
			this.getSqlMapClientTemplate().update(
					"t_Member_Card.updateCouponAmount", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List getMyCardByUserToken(String userToken) throws Exception {
		String sqlStr = "";
		try {
			sqlStr = "t_Member_Card.getMyCardByUserToken";
			return queryForList(sqlStr, userToken);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List selectByIds(List ids) throws BaseException {
		return queryForList("t_Member_Card.selectByIds", ids);
	}

	public Collection selectByUserToken(String userToken) throws BaseException {
		if (userToken == null)
			return null;
		try {
			return queryForList("t_Member_Card.selectByUserToken", userToken);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public int saveReturnKey(MemberCardVO memberCardVO) throws BaseException {
		try {
			Object obj = this.getSqlMapClientTemplate().insert(
					"t_Member_Card.saveReturnKey", memberCardVO);
			return obj == null ? 0 : Integer.parseInt(obj.toString());
		} catch (Exception e) {
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public API_MemberCardVO selectApiMemberCardVOByMap(Map paramMap) throws BaseException {
		try {
			return (API_MemberCardVO) this.getSqlMapClientTemplate()
					.queryForObject("t_Member_Card.selectApiMemberCardVOByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public void insertVOs(MemberCardVO[] memberCardVOs) throws BaseException {
		if (memberCardVOs == null)
			return ;
		try {
			Map param = Maps.newHashMap();
			param.put("costs", Arrays.asList(memberCardVOs));
			this.getSqlMapClientTemplate().insert("t_Member_Card.betchAdd", param);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public List<MemberCardVO> selectByFeedbackCount(Map paramMap) throws BaseException {
		return (List<MemberCardVO>)queryForList("t_Member_Card.selectByFeedbackCount", paramMap);
	}
	
	public boolean updateToMobileUser(Integer userId) throws BaseException{
		try {
			this.getSqlMapClientTemplate().update(
					"t_Member_Card.updateToMobileUser", userId);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}	
	}

}